<?php
$pageTitle = "个人中心";
$currentPage = "center";
if (!isset($_COOKIE['user_id'])) {
    header("Location: ../main/login.php");
    exit();
}

$user_id = $_COOKIE['user_id'];

include('../resource/navbar.php');
include('../resource/db/db.php');

$sql = "SELECT * FROM users WHERE user_id='$user_id' AND is_deleted=FALSE";
$result = $conn->query($sql);
$user = $result->fetch_assoc();
?>

<!DOCTYPE html>
<html>

<head>
    <title>个人中心</title>
    <link rel="stylesheet" href="../resource/css/styles.css">
</head>

<body>
    <div class="container">
        <h2>个人中心</h2>
        <?php if ($user) : ?>
            <div class='container' style='padding-bottom:10px;'>
                <div class='user-info'>
                    <img src='../uploads/avatars/<?= htmlspecialchars($user['avatar']) ?>' 
                    alt='头像' style='width: 150px; height: 150px; border-radius: 50%;'><br>
                    <strong>用户名:</strong> <?= htmlspecialchars($user['username']) ?><br>
                    <strong>邮箱:</strong> <?= htmlspecialchars($user['email']) ?><br>
                    <strong>角色:</strong> <?= htmlspecialchars($user['role']) ?><br>
                </div>
            </div>
            <p style='text-align: center;'><a href='../main/logout.php' class='btn btn-danger'>退出登录</a></p>

            <h3>修改个人信息</h3>
            <div class='container' style='margin-top:10px;'>
                <form action='update.php' method='post' enctype='multipart/form-data'>
                    <div class="form-group">
                        <label for="username">用户名:</label>
                        <input type="text" id="username" name="username" class="form-control" value="<?= htmlspecialchars($user['username']) ?>">
                    </div>
                    <div class="form-group">
                        <label for="email">邮箱:</label>
                        <input type="text" id="email" name="email" class="form-control" value="<?= htmlspecialchars($user['email']) ?>">
                    </div>
                    <div class="form-group">
                        <label for="avatar">头像:</label>
                        <input type="file" id="avatar" name="avatar" class="form-control" accept="image/*">
                    </div>
                    <button type="submit" class="btn btn-primary">保存更改</button>
                </form>
            </div>
        <?php else : ?>
            <p>用户不存在或已被删除。</p>
        <?php endif; ?>
    </div>
</body>

</html>